package com.product.id;

import com.product.utils.ThreadLocalUtil;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * @author Hanhuiyong
 * @version 1.0.0
 * @ClassName IdService.java
 * @Description
 * @createTime 2021年09月13日 16:02:00
 */
@Service
public class IdService {

    Long getNextIemId() {
        try {
            String time = new SimpleDateFormat("yyMMdd").format(new Date());

            String type = "2";

            Long merchantId = ThreadLocalUtil.merchantIdThreadLocal.get();

            int dbIndex = (int) (merchantId % 2 + 1);
            return Long.parseLong(
                    type + dbIndex + time + String.format("%04d", new Random().nextInt(10000)));
        } finally {
            ThreadLocalUtil.merchantIdThreadLocal.remove();
        }
    }
}
